package com.lc.afbx.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.activity.TeamActivity;
import com.lc.afbx.activity.TeamPolicyActivity;
import com.lc.afbx.conn.GetApiTeamSearch;

import java.util.List;

/**
 * Created by Administrator on 2016/6/8.
 */
public class TeamAdapter extends BaseAdapter {
    private final String preName;
    private List<GetApiTeamSearch.Data> dataList;
    private LayoutInflater inflater;
    private Context context;

    public TeamAdapter(Context context, List<GetApiTeamSearch.Data> dataList, String preName) {
        inflater = LayoutInflater.from(context);
        this.preName = preName;
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.item_team, null);
            BaseApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder = new ViewHolder();

            holder.code = (TextView) convertView.findViewById(R.id.team_code);
            holder.nickname = (TextView) convertView.findViewById(R.id.name);
            holder.level = (TextView) convertView.findViewById(R.id.type);
            holder.sectors = (TextView) convertView.findViewById(R.id.sectors);
            holder.btnSearch = (Button) convertView.findViewById(R.id.btn_chaxun);
            holder.btnSearch.setFocusable(false);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.level.setText(dataList.get(position).level);
        holder.code.setText(dataList.get(position).code);
        holder.nickname.setText(dataList.get(position).nickname);
        holder.sectors.setText(dataList.get(position).sectors);

        holder.btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TeamPolicyActivity.class);

                intent.putExtra("tid", dataList.get(position).id);

                context.startActivity(intent);

            }
        });

        if (dataList.get(position).team_state.equals("true")) {

            convertView.setBackgroundResource(R.color.selectView);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TeamActivity.class);

                    Bundle bundle = new Bundle();

                    bundle.putString("preName", preName + "-");
                    bundle.putString("otherInfoId", dataList.get(position).id);

                    intent.putExtras(bundle);

                    context.startActivity(intent);
                }
            });
        }

        return convertView;
    }

    class ViewHolder {

        TextView code, nickname, level, sectors;

        Button btnSearch;
    }
}
